<!doctype html>
<html>
<?php
	include('config.php');
	include('display.php');
	dbconnect();
	$admin = ($HTTP_COOKIE_VARS["admin"]=="benvi"); // cookie admin
?>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta http-equiv="Content-Language" content="fr">
<script language="JavaScript" src="ajax.js"></script>
<script language="JavaScript" src="cookies.js"></script>
<link rel="stylesheet" type="text/css" href='http://fonts.googleapis.com/css?family=Permanent+Marker'>
<link rel="stylesheet" type="text/css" media="all" href="<?php h(); ?>style.css" />
<link rel="alternate" type="application/rss+xml" title="RSS 2.0" href="<?php h(); ?>feed.rss" />
<?php
	$pagetitle = "Hurmpf... ";
	if(!empty($_GET['p']) && is_numeric($_GET['p']))
	{
		$sql = 'SELECT titre FROM posts WHERE id='.$_GET['p'];
		$req = mysql_query($sql) or die('Erreur SQL !<br>'.$sql2.'<br>'.mysql_error());
		$row = mysql_fetch_array($req);
		$pagetitle .= $row['titre'];
	}
	elseif(!empty($_GET['q'])) $pagetitle .= 'Recherche "'.stripslashes($_GET['q']).'"';
	elseif(isset($_GET['archives'])) $pagetitle .= 'Archives';
	echo "<title>$pagetitle</title>";
?></head>
<body>
<div id="mainwrap">

<div id="header">
	<div class="wrap">
		<div id="hurmpf"><a href="<?php h(); ?>">Hurmpf</a></div>
	</div> <!-- wrap -->
</div> <!-- header -->

<div class="wrap">
	<div id="sidebar">
		<a href="<?php h(); ?>"><img id="mainpic" src="<?php h(); ?>icon-totoro.jpg" width="150" height="150" alt="Hurmpf..."></a>
		<form method="get" id="searchform" action="<?php h(); ?>"><div>
			<input type="text" value="" name="q" id="q" placeholder="recherche..." /><input type="submit" id="searchsubmit" value="?" />
		</div></form>
		<ul>
			<li><a href="<?php h(); ?>" title="Voir la page d'accueil">Accueil</a></li>
			<li><a href="<?php h(); ?>?archives" title="Voir les anciens billets">Archives</a></li>
			<li><a href="<?php h(); ?>?liens" title="Voir les liens externes">Liens</a></li>
			<li><a href="<?php h(); ?>feed.rss"><img src="<?php h(); ?>rss.png" width="48" height="48" title="Flux RSS"></a></li>
		</ul>
	</div>
	<div id="main">
<?php
	if(!empty($_GET['p']) && is_numeric($_GET['p'])) // un post seul
	{
		$p = $_GET['p'];
		$sql = 'SELECT * FROM posts WHERE id='.$p;
		$req = mysql_query($sql) or die('Erreur SQL !<br>'.$sql.'<br>'.mysql_error()); 
		display($req,true,true,$admin);
		$nn = 0;
	}
	elseif(!empty($_GET['q']))         include("recherche.php");
	elseif(isset($_GET['archives']))   include("archives.php");
	elseif(isset($_GET['notfound']))   include("notfound.php");
	elseif(isset($_GET['liens']))      include("liens.php");
	elseif(isset($_GET['preview']))    include("preview.php");
	elseif(isset($_GET['avatars']))    include("avatars.php");
	elseif(isset($_GET['wallpapers'])) include("wallpapers.php");
	elseif(isset($_GET['preview']))    include("preview.php");
	else
	{
		$now = date('Y-m-d');
		if($local==false)
		{
			$sql = "SELECT count(id) as n FROM posts WHERE TIMESTAMPDIFF(DAY,date,'$now')<=7";
			$req = mysql_query($sql) or die('Erreur SQL !<br>'.$sql.'<br>'.mysql_error());
			$nn = mysql_result($req,0,"n");
			if($nn==0) $nn = 1;
		}
		else $nn = 3;
		// derniers posts
		$sql = 'SELECT * FROM posts ORDER BY date DESC LIMIT '.$nn;
		$req = mysql_query($sql) or die('Erreur SQL !<br>'.$sql.'<br>'.mysql_error()); 
		display($req,false,false,$admin);
	}
	if(isset($nn))
	{
		// blocs derniers billets et commentaires
		echo '<div id="lastposts">'."\n";
		echo '<h3>Derniers billets</h3>'."\n";
		echo '<ul>'."\n";
		$sql = 'SELECT id, date, titre FROM posts ORDER BY date DESC LIMIT '.$nn.', 5';
		$req = mysql_query($sql) or die('Erreur SQL : '.mysql_error()); 
		while($row = mysql_fetch_array($req))
		{
			$id = $row['id'];
			$dateString = date('d/m/Y, H:i',strtotime($row['date']));
			$titre = stripslashes($row['titre']);
			echo '<li><span class="smalldate">'.$dateString.'</span> : ';
			echo '<a href="?p='.$id.'">'.$titre."</a></li>\n";
		}
		echo '</ul></div>'."\n";
		echo '<div id="lastcomms">'."\n";
		echo '<h3>Derniers commentaires</h3>'."\n";
		echo '<ul>'."\n";
		$sql = 'SELECT id, ref, date, nom FROM comments ORDER BY date DESC LIMIT 5';
		$req = mysql_query($sql) or die('Erreur SQL : '.mysql_error()); 
		while($row = mysql_fetch_array($req))
		{
			$id = $row['id'];
			$ref = $row['ref'];
			$dateString = date('d/m/Y, H:i',strtotime($row['date']));
			$nom = stripslashes($row['nom']);
			$sql2 = 'SELECT titre FROM posts WHERE id='.$ref;
			$req2 = mysql_query($sql2) or die('Erreur SQL : '.mysql_error()); 
			$row2 = mysql_fetch_array($req2);
			$titre = stripslashes($row2['titre']);
			echo '<li><span class="smalldate">'.$dateString.'</span> : ';
			echo '<a href="?p='.$ref.'#'.$id.'">'.$nom.' sur "'.$titre.'"</a></li>'."\n";
		}
		echo '</ul></div>'."\n";
	}
?>
	</div>
</div> <!-- wrap -->

</div> <!-- mainwrap -->

<div id="footer">
	<div class="wrap">
		<div id="footerpic"><img src="<?php h(); ?>icon-yotsuba.png" width="103" height="84"></div>
		<p>
			ただ、それだけ。
			<a rel="license" href="http://creativecommons.org/licenses/by-nc-sa/3.0/"><img alt="Contrat Creative Commons" style="border-width:0" src="http://i.creativecommons.org/l/by-nc-sa/3.0/80x15.png" /></a>
			<?php if(!$admin) { ?>
			<!-- stats hurmpf -->
			<script language="JavaScript" type="text/javascript" src="http://hurmpf.fr/stats/record.js"></script>
			<script language="JavaScript" type="text/javascript">record("hurmpf");</script>
			<!-- /stats hurmpf -->
			<!-- stats google -->
			<script type="text/javascript">
				var _gaq = _gaq || [];
				_gaq.push(['_setAccount', 'UA-5632666-1']);
				_gaq.push(['_trackPageview']);
				(function() {
					var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
					ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
					var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
				})();
			</script>
			<!-- /stats google -->
		</p>
		<noscript><img src="http://hurmpf.fr/stats/record.php?id=hurmpf" width="0" height="0" style="display:none" /></noscript>
		<?php } ?>
	</div> <!-- wrap -->
</div>
<?php mysql_close(); ?>
</body>
</html>